package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.vo.NursingPlanAllVo;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author tomorrow
 * @date 2024-12-30
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询护理计划基本信息
        // 1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //查询护理计划项目计划关联表
        List<NursingProjectPlanVo> projectPlanList = nursingPlanMapper.selectProjectPlanByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlanList);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    //开启事务
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {

        //创建nursingPlan对象
        NursingPlan nursingPlan = new NursingPlan();

        //调用工具类，直接将dto中的属性拷贝到实体中
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        System.out.println(nursingPlan);
        //设置创建时间
        nursingPlan.setCreateTime(DateUtils.getNowDate());

        //保存护理计划的基本信息
        save(nursingPlan);

        //保存护理项目与护理计划的关联表
        // 批量保存护理项目计划关系
        int count = nursingPlanMapper.insertBatch(nursingPlanDto.getProjectPlans(),nursingPlan.getId());
        return count >0? 1: 0;

    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //先获取到新的护理计划信息关联列表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        //判断新的关联列表是否为空
        if(projectPlans != null && !projectPlans.isEmpty()){

            //不为空，删除其前关联列表
            nursingPlanMapper.deleteByPlanId(nursingPlanDto.getId());

            //批量保存新的关联列表
            int count = nursingPlanMapper.insertBatch(nursingPlanDto.getProjectPlans(),nursingPlanDto.getId());

        }

        //同时修改护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        return updateById(nursingPlan)? 1: 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids)
    {
        // 批量删除护理计划关联的护理项目表
        nursingPlanMapper.deleteByPlanIds(Arrays.asList(ids));

        //删除护理计划基本信息
        return nursingPlanMapper.deleteBatchIds(Arrays.asList(ids));

    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return nursingPlanMapper.deleteById(id);
    }

    /**
     * 查询全部护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanAllVo> selectAllList() {
        List<NursingPlanAllVo> nursingPlanAllVos = nursingPlanMapper.selectAllList();

        System.out.println(nursingPlanAllVos);
        return nursingPlanAllVos;
    }
}
